<template>
  <view class="container">
    <uni-card>
      <!-- 搜索区域 -->
      <view class="uni-search">
        <uni-search-bar
          v-model="queryParams.postName"
          placeholder="请输入岗位名称"
          @confirm="handleQuery"
        />
      </view>
      
      <!-- 操作按钮区域 -->
      <view class="uni-group">
        <button type="primary" @click="handleAdd" size="mini">新增</button>
      </view>

      <!-- 列表区域 -->
      <uni-list>
        <uni-list-item v-for="(item, index) in postList" :key="index"
          :title="item.postName"
          :note="item.postCode"
          :rightText="item.status === '0' ? '正常' : '停用'"
          clickable
          @click="handleUpdate(item)"
        >
          <template v-slot:footer>
            <view class="uni-group">
              <button type="primary" size="mini" @click.stop="handleUpdate(item)">修改</button>
              <button type="warn" size="mini" @click.stop="handleDelete(item)" style="margin-left: 10px;">删除</button>
            </view>
          </template>
        </uni-list-item>
      </uni-list>
    </uni-card>

    <!-- 添加或修改岗位弹窗 -->
    <uni-popup ref="popup" type="dialog">
      <uni-popup-dialog
        :title="title"
        mode="input"
        :value="form.postName"
        placeholder="请输入岗位名称"
        @confirm="submitForm"
      >
        <uni-forms ref="form" :model="form" :rules="rules">
          <uni-forms-item label="岗位名称" required name="postName">
            <uni-easyinput v-model="form.postName" placeholder="请输入岗位名称"/>
          </uni-forms-item>
          <uni-forms-item label="岗位编码" required name="postCode">
            <uni-easyinput v-model="form.postCode" placeholder="请输入岗位编码"/>
          </uni-forms-item>
          <uni-forms-item label="显示顺序" required name="postSort">
            <uni-easyinput v-model="form.postSort" type="number" placeholder="请输入显示顺序"/>
          </uni-forms-item>
          <uni-forms-item label="岗位状态" name="status">
            <uni-data-checkbox
              v-model="form.status"
              :localdata="[
                {text: '正常', value: '0'},
                {text: '停用', value: '1'}
              ]"
            />
          </uni-forms-item>
          <uni-forms-item label="备注" name="remark">
            <uni-easyinput v-model="form.remark" type="textarea" placeholder="请输入备注"/>
          </uni-forms-item>
        </uni-forms>
      </uni-popup-dialog>
    </uni-popup>
  </view>
</template>

<script>
import { listPost, getPost, delPost, addPost, updatePost } from '@/api/system/post'

export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 岗位列表
      postList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        postCode: undefined,
        postName: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postName: {
          rules: [{
            required: true,
            errorMessage: '岗位名称不能为空'
          }]
        },
        postCode: {
          rules: [{
            required: true,
            errorMessage: '岗位编码不能为空'
          }]
        },
        postSort: {
          rules: [{
            required: true,
            errorMessage: '显示顺序不能为空'
          }]
        }
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listPost(this.queryParams).then(response => {
        this.postList = response.rows;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.title = "添加岗位";
      this.$refs.popup.open();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getPost(row.postId).then(response => {
        this.form = response.data;
        this.title = "修改岗位";
        this.$refs.popup.open();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.form.validate().then(res => {
        if (this.form.postId != undefined) {
          updatePost(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.$refs.popup.close();
            this.getList();
          });
        } else {
          addPost(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.$refs.popup.close();
            this.getList();
          });
        }
      }).catch(error => {
        console.log('表单错误信息：', error);
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      uni.showModal({
        title: '提示',
        content: '是否确认删除名称为"' + row.postName + '"的数据项?',
        success: res => {
          if (res.confirm) {
            delPost(row.postId).then(() => {
              this.getList();
              this.$modal.msgSuccess("删除成功");
            })
          }
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        status: '0',
        remark: undefined
      };
    }
  }
};
</script>

<style>
.container {
  padding: 20rpx;
}
.uni-search {
  padding: 10rpx 0;
}
.uni-group {
  margin: 20rpx 0;
  display: flex;
  justify-content: flex-end;
}
</style> 